/**
 * Copyright(c): 2017 com.mjduan All rights reserved.
 * 项目名：springcloud-eureka
 * 注意：未经作者允许，不得外传
 */
package com.mjduan.springcloud.project.servicea.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dmj1161859184@126.com mjduan 2018-05-31 15:04
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/serviceA")
public class ServiceAController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/hello")
    public String hello() {
        LOGGER.info("hello");
        return "Hello, this is serviceA provider";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        Integer result = a + b;
        LOGGER.info("/add result:{}, host{}:port{}, serviceId:{}", result, serviceInstance.getHost(), serviceInstance.getPort(), serviceInstance.getServiceId());
        return result;
    }
}
